package tech.xs.common.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import tech.xs.common.sys.domain.bo.CurrLoginUserBo;
import tech.xs.common.framework.util.AuthUtil;

import java.time.LocalDateTime;

@Component
@Order(10200)
public class BaseEntityMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createdTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
        CurrLoginUserBo currLoginUser = AuthUtil.getCurrLoginUser();
        String currUserName = "";
        if (currLoginUser != null) {
            currUserName = currLoginUser.getUserName();
        }
        this.strictInsertFill(metaObject, "createdBy", String.class, currUserName);
        this.strictInsertFill(metaObject, "updatedBy", String.class, currUserName);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
        CurrLoginUserBo currLoginUser = AuthUtil.getCurrLoginUser();
        String currUserName = "";
        if (currLoginUser != null) {
            currUserName = currLoginUser.getUserName();
        }
        this.strictInsertFill(metaObject, "updatedBy", String.class, currUserName);
    }
}
